package com.mew.sas.controller;

import com.mew.common.enm.ExitType;
import com.mew.common.rest.sas.EnterGameSasRequest;
import com.mew.common.rest.sas.ExitGameSasRequest;
import com.mew.common.rest.sas.LoginSasRequest;
import com.mew.common.rest.sas.LogoutSasRequest;
import com.mew.sas.service.GoldRankService;
import com.mew.sas.service.OnlineGiftSasService;
import com.zebra.common.concurrent.ZebraExecutorManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录 登出
 *  进入 退出 游戏
 */
@RestController
@Slf4j
public class GameEESasController {

    @Autowired
    private OnlineGiftSasService onlineGiftSasService;
    @Autowired
    private GoldRankService goldRankService;

    @PostMapping(value = "/ll/login")
    @ResponseBody
    public Integer loginSas(@RequestBody LoginSasRequest request){
        ZebraExecutorManager.getInstance().getDefaultExecutor().execute(() ->{
            //TODO 处理--登录-- 统计代码
            log.info("Login SAS {}", request.toString());
            onlineGiftSasService.enter(request.getUserId());
        });
        return 1;
    }


    @PostMapping(value = "/ll/logout")
    @ResponseBody
    public Integer logoutSas(@RequestBody LogoutSasRequest request){
        ZebraExecutorManager.getInstance().getDefaultExecutor().execute(() ->{
            //TODO 处理 --登出整个APP-- 统计代码
            log.info("Logout SAS {}", request.toString());
            onlineGiftSasService.exit(request.getUserId(), ExitType.Exit);

        });
        return 1;
    }


    @PostMapping(value = "/ee/enter")
    @ResponseBody
    public Integer enterGame(@RequestBody EnterGameSasRequest request){
        ZebraExecutorManager.getInstance().getDefaultExecutor().execute(() ->{
            //TODO 处理 --进入游戏-- 统计代码
            log.info("EnterGame SAS {}", request.toString());
        });
        return 1;
    }

    @PostMapping(value = "/ee/exit")
    @ResponseBody
    public Integer exitGame(@RequestBody ExitGameSasRequest request){
        ZebraExecutorManager.getInstance().getDefaultExecutor().execute(() ->{
            //TODO 处理 --退出游戏-- 统计代码
            //每日金币记录
            try {
                log.info("ExitGame SAS {}", request.toString());
                goldRankService.userWin(request.getUserId(),request.getBet(), request.getPayment());
            } catch (Exception e) {
                log.error("ExitGasme sas error",e);
            }

        });
        return 1;
    }
}
